var status = -1;

function action(mode, type, selection) {
    if (mode == 1) {
        status++;
    } else {
        if (status == 0) {
            cm.dispose();
        }
        status--;
    }
    if (status == 0) {
        cm.sendSimple("#b#L1#我要到冰雪峽谷(個人)(任務)#l\r\n#L2#我要到冰雪峽谷(組隊)#l\r\n\r\n#L3#升級雷克斯的紅耳環#l\r\n#L4#升級雷克斯的藍耳環#l\r\n#L5#升級雷克斯的綠耳環#l#k");
    } else if (status == 1) {
        if (selection == 1) {
            cm.warp(921120000, 0);
        } else if (selection == 2) {
            if (cm.getPlayer().getParty() == null || !cm.isLeader()) {
                cm.sendOk("請透過隊長來找我對話。");
            } else {
                var party = cm.getPlayer().getParty().getMembers();
                var mapId = cm.getPlayer().getMapId();
                var next = true;
                var size = 0;
                var it = party.iterator();
                while (it.hasNext()) {
                    var cPlayer = it.next();
                    var ccPlayer = cm.getPlayer().getMap().getCharacterById(cPlayer.getId());
                    cm.playerMessage(ccPlayer.getLevel());
                    if (ccPlayer == null || ccPlayer.getLevel() < 120) {
                        next = false;
                        break;
                    }
                    size += (ccPlayer.isGM() ? 4 : 1);
                }
                if (next && size >= 2) {
                    var em = cm.getEventManager("Rex");
                    if (em == null) {
                        cm.sendOk("I don't wanna see Rex at the moment. Please try again later.");
                    } else {
                        var prop = em.getProperty("state");
                        if (prop.equals("0") || prop == null) {
                            em.startInstance(cm.getPlayer().getParty(), cm.getPlayer().getMap(), 200);
                        } else {
                            cm.sendOk("其他隊伍正在挑戰中，請稍候再試。");
                        }
                    }
                } else {
                    cm.sendOk("隊員等級必須大於120級，而且隊員人數必須有2人(含)以上。");
                }
            }
        } else if (selection == 3) {
            if (cm.haveItem(1032078, 1)) {
                if (!cm.canHold(1032103, 1)) {
                    cm.sendOk("請檢查裝備欄位空間。");
                } else if (cm.haveItem(4001530, 20)) { //TODO JUMP
                    cm.gainItem(1032103, 1);
                    cm.gainItem(4001530, -20);
                } else {
                    cm.sendOk("請收集20個侏儒戰士的證明。");
                }
            } else {
                cm.sendOk("你沒有雷克斯的紅耳環，無法升級。");
            }
        } else if (selection == 4) {
            if (cm.haveItem(1032079, 1)) {
                if (!cm.canHold(1032104, 1)) {
                    cm.sendOk("請檢查裝備欄位空間。");
                } else if (cm.haveItem(4001530, 20)) { //TODO JUMP
                    cm.gainItem(1032104, 1);
                    cm.gainItem(4001530, -20);
                } else {
                    cm.sendOk("請收集20個侏儒戰士的證明。");
                }
            } else {
                cm.sendOk("你沒有雷克斯的藍耳環，無法升級。");
            }
        } else if (selection == 5) {
            if (cm.haveItem(1032077, 1)) {
                if (!cm.canHold(1032102, 1)) {
                    cm.sendOk("請檢查裝備欄位空間。");
                } else if (cm.haveItem(4001530, 20)) { //TODO JUMP
                    cm.gainItem(1032102, 1);
                    cm.gainItem(4001530, -20);
                } else {
                    cm.sendOk("請收集20個侏儒戰士的證明。");
                }
            } else {
                cm.sendOk("你沒有雷克斯的綠耳環，無法升級。");
            }
        }
        cm.dispose();
    }
}